<template>
  <!--学生选题-->
  <div class="student-topic">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />
    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.studentTopic')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
            <el-button type="primary" @click="onChoose">{{$t('graduationDesign.chooseSubject')}}</el-button>
          </div>
        </div>
        <edu-table ref="studentTopicTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.instructor')" prop="teacherName" min-width="130">
            <template slot-scope="scope">
              <span>{{`${scope.row.teacherName}（${scope.row.teacherNum}）`}}</span>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.jobTitle')" prop="titleName" min-width="80"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.optCollege')" prop="collegeIdI18n" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.optSubject')" prop="subjectIdI18n" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.optDirection')" prop="direction" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectSource')" prop="projectSource" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectNature')" prop="projectNature" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.socialPractice')" prop="practiceSocial" min-width="140"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.applyReason')" prop="applyDesc" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.reviewStatus')" min-width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'WaitCommit'" type="info">{{$t('graduationDesign.draft')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Process'" type="info">{{$t('graduationDesign.pendingReview')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Checking'" type="info">{{$t('graduationDesign.underReview')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">{{$t('graduationDesign.rejected')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'End'" type="success">{{$t('graduationDesign.passed')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="120">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
              <el-button type="text" @click="onCancel(scope.row)" v-if="scope.row.infoType !== 'stuInfo' && scope.row.status === 'Process'">{{$t('graduationDesign.cancelSubject')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>
    <div v-if="indexKey === 'CHOOSE'">
      <!--选择课题-->
      <ChooseSubject @choose-submit="chooseSubmit" @choose-cancel="chooseCancel" @choose-view="chooseView"></ChooseSubject>
    </div>
    <div v-if="indexKey === 'VIEW_TUTOR' || indexKey === 'VIEW_TUTOR_SELECT'">
      <!--查看导师课题详情-->
      <TopicInfo :actionType="actionType" :basicInfoForm="topicInfoForm"></TopicInfo>
    </div>
    <div v-if="indexKey === 'VIEW_STU'">
      <!--查看学生课题详情-->
      <StuSubjectInfo :subjectForm="topicInfoForm"></StuSubjectInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/studentTopic";
import UTILS from 'common/src/utils/utils';
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'studentTopic',
  data() {
    return {
      api: API,
      getList: '/api/designservice/optStudentInfo/queryByPageOptStu',
      queryForm: {},
      indexKey: 'MAIN',
      studentSelectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.studentTopic'),
        key: "MAIN",
        children: [
          {
            key: "VIEW_TUTOR",
            label: this.$t('graduationDesign.viewSubject')
          },
          {
            key: "VIEW_STU",
            label: this.$t('graduationDesign.viewSubject')
          },
          {
            key: "CHOOSE",
            label: this.$t('graduationDesign.chooseSubject'),
            children: [
              {
                key: "VIEW_TUTOR_SELECT",
                label: this.$t('graduationDesign.viewSubject')
              }
            ]
          }
        ]
      },
      loading: false,
      topicInfoForm: {}, //课题详情
      actionType: ''
    };
  },
  watch: {
    indexKey: function (newVal, oldVal) {
      if (newVal === "MAIN") {
        this.searchList();
      }
    }
  },
  mounted() {
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.studentTopicTable.search(this.queryForm);
    },
    handleSelectChange(val) {
      this.studentSelectedData = val;
    },
    // 选择课题
    onChoose() {
      const data = this.$refs.studentTopicTable.getData() || [];
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        if (element.status === "End") {
          let content = "";
          if (element.infoType === "stuInfo") {
            content = this.$t('graduationDesign.chooseSubjectStuAlertTips');
          } else {
            content = this.$t('graduationDesign.chooseSubjectTeacherAlertTips');
          }
          this.$eduAlert(content, this.$t('graduationDesign.prompt'));
          return;
        }
      }
      this.indexKey = "CHOOSE";
    },
    // 查看 导师/学生课题详情
    onView(row) {
      const params = { ...row };
      this.api.getSubjectInfo(params).then(res => {
        this.indexKey = "VIEW_TUTOR";
        this.actionType = "VIEW_STU_TOPIC_TUTOR";
        if (row.infoType === "stuInfo") {
          this.actionType = "VIEW_STU";
          this.indexKey = "VIEW_STU";
        }
        this.topicInfoForm = { ...res.data };
        this.topicInfoForm.grade = res.data.grade + '';
      }).catch();
    },
    // 取消课题
    onCancel(row) {
      const contentTips = `<span style="color: #409EFF;">"${row.projectName}"</span>`;
      this.$eduConfirm(UTILS.i18nReplace(this.$t('graduationDesign.cancelSubjectTips'), { contentTips: contentTips }), this.$t('graduationDesign.cancelSubject'), {
        dangerouslyUseHTMLString: true
      }).then(() => {
        const params = { ...row };
        this.api.cancelApply(params).then(res => {
          this.$message.success(this.$t('graduationDesign.cancelSuccess'));
          this.searchList();
        }).catch();
      }).catch(() => { });
    },
    // 选择课程 查看导师课题详情
    chooseView(data) {
      this.indexKey = "VIEW_TUTOR_SELECT";
      this.actionType = "VIEW_STU_TOPIC_TUTOR";
      this.topicInfoForm = { ...data };
      this.topicInfoForm.grade = data.grade + '';
    },
    chooseSubmit(data) {
      this.indexKey = "MAIN";
    },
    chooseCancel() {
      this.indexKey = "MAIN";
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.studentSelectedData });
      this.api.export(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "学生选题.xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo'),
    ChooseSubject: () => import('./components/chooseSubject'),
    StuSubjectInfo: () => import('./components/stuSubjectInfo')
  }
}
</script>
<style lang="scss" scoped>
</style>
